#!/usr/bin/env python
# ----------------------------------------------------------------------------
# pyglet
# Copyright (c) 2009-2011 Mihai Maruseac (mihai.maruseac@gmail.com)
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions 
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#  * Neither the name of pyglet nor the names of its
#    contributors may be used to endorse or promote products
#    derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------

'''Displays text using bitmap fonts

This example demonstrates:

 * the use of a Label to draw text on the screen

'''

import pyglet
from pyglet.gl import *
from pyglet.window import *
import math

window = pyglet.window.Window(width=640, height=480, resizable=True,
			style=Window.WINDOW_STYLE_DIALOG)
cnt1 = 0
cnt2 = 0 # 2 variables used for drawing the text (color and position)
label = pyglet.text.Label() # the drawing area

@window.event
def on_resize(width, height):
    if height == 0:
        height = 1
    glViewport(0, 0, width, height)
    glMatrixMode(gl.GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, width / float(height), .1, 100.0)
    glMatrixMode(gl.GL_MODELVIEW)
    glLoadIdentity()
    #return pyglet.event.EVENT_HANDLED <- apparently this doesn't work

def init():
    glShadeModel(gl.GL_SMOOTH)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0)
    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LEQUAL)
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

@window.event
def on_draw():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glTranslatef(0.0, 0.0, -1.0)
    x_print = (-0.45 + 0.05 * math.cos(cnt1)) * window.width + window.width//2
    y_print = (0.32 * math.sin(cnt2)) * window.height + window.height//2
    glPrint(x_print, y_print, "Python + Nehe = Pyglet - %7.2f, %5.1f",\
            (cnt1, cnt2))

def update(dt):
    """
    Updating the variables used for colors and positions
    
    """
    global cnt1
    global cnt2
    cnt1 += 0.051
    cnt2 += 0.005

pyglet.clock.schedule(update)

def glPrint(x, y, fmt, args):
    """
    Prints a string to the screen using the format specified with the needed
    arguments.
        x, y - the raster position
        fmt - the format string
        args - a tuple of the arguments
    
    """
    label.x = x
    label.y = y
    label.font_name = 'Times New Roman'
    label.font_size = 24
    label.color = (int(255 * math.cos(cnt1)), int(255 * math.sin(cnt2)), \
                   int(255 * math.cos(cnt1 + cnt2)), 255)
    label.text = fmt %args
    label.draw()
    pass

init()
pyglet.app.run()
